﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zyg.SSO.Core.Business;
using Zyg.Utility.DBUtility;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class SqlProvider : DALProvider
    {
        public override Zyg.SSO.Core.Business.SysSetting SelectCurrentSetting()
        {
            Zyg.Utility.DBUtility.SqlServerUtility db = new Zyg.Utility.DBUtility.SqlServerUtility();

            SqlDataReader reader = db.ExecuteSPReader("usp_SelectTop1SettingByDateCreated");

            if (reader != null && !reader.IsClosed && reader.Read())
            {
                Business.SysSetting setting = new SysSetting();

                if (!reader.IsDBNull(1)) setting.RegisterCheck = reader.GetBoolean(1);
                if (!reader.IsDBNull(2)) setting.PageSize = reader.GetInt32(2);
                if (!reader.IsDBNull(3)) setting.Copyright = reader.GetString(3);
                if (!reader.IsDBNull(4)) setting.DateCreated = reader.GetDateTime(4);

                reader.Close();

                return setting;
            }
            else
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();

                return null;
            }
        }

        public override void InsertSysSetting(Zyg.SSO.Core.Business.SysSetting setting)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, setting.Id);
            db.AddParameter("@RegisterCheck", SqlDbType.Bit, setting.RegisterCheck);
            db.AddParameter("@PageSize", SqlDbType.Int, setting.PageSize);
            db.AddParameter("@Copyright", SqlDbType.VarChar, setting.Copyright);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, setting.DateCreated);

            db.ExecuteSP("usp_InsertSetting");
        }
    }
}
